<?php
/**
 * @Name: NodeLogic.php
 * @Author: yashuai<1762910894@qq.com>
 */

namespace app\common\logic\rbac;

use app\common\logic\BaseCurdLogic;
use app\common\model\rbac\NodeModel;
use HugCode\PhpUnits\Frame\Code\ConstCode;

class NodeLogic extends BaseCurdLogic
{

    /**
     * @Desc 设置模型
     * @return string
     * @author yashuai<1762910894@qq.com>
     */
    protected function setModel(): string
    {
        return NodeModel::class;
    }

    /**
     * @Desc 数据转为表结构数据
     * @param array $params
     * @param string|null $type
     * @param array|null $row
     * @return array
     * @author yashuai<1762910894@qq.com>
     */
    protected function getParamsTableData(array $params = [], ?string $type = '', ?array $row = []): array
    {
        $parentID = _array_field($params, 'ParentID', 0);
        if (!empty($parentID) && empty($this->get(['NodeID' => $parentID]))) {
            _throw('上级菜单不存在', 106002);
        }

        $data = [
            'ParentID'       => $parentID,
            'NodeName'       => $params['NodeName'],
            'ControllerName' => $params['ControllerName'],
            'ActionName'     => $params['ActionName'],
            'EventType'      => $params['ControllerName'] . '.' . $params['ActionName'],
            'NodeType'       => $params['NodeType'],
            'NodeShowType'   => $params['NodeShowType'],
            'NodeOrder'      => $params['NodeOrder'],
            'NodeStatus'     => ConstCode::BASIC_STATUS_NORMAL,
        ];
        if (isset($params['NodeIcon'])) {
            $data['NodeIcon'] = $params['NodeIcon'];
        }
        return $data;
    }

}
